﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_ListLottery : WebControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 3)
        {
            lnkAdd.Enabled = false;
            lnkAddBottom.Enabled = false;
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkAdd.Enabled = true;
            lnkAddBottom.Enabled = true;
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;
        }

        if (IsPostBack) return;

        ViewState["SortExpression"] = "ServiceDetailID";
        ViewState["SortDirection"] = SortDirection.Descending;

        this.BindData();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        // Search
        DataTable dt = ServiceDetailsManager.SearchInfo(txtCode.Text.Trim());
        if (dt != null)
        {
            this.grvServiceDetail.DataSource = dt;
            this.grvServiceDetail.DataBind();
        }
        else
        {
            this.grvServiceDetail.DataSource = null;
            this.grvServiceDetail.DataBind();
        }
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=DetailLottery");
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        // Search all checked rows to delete
        foreach (GridViewRow r in this.grvServiceDetail.Rows)
        {
            // If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            // If checkbox is checked then delete
            if (ServiceDetailsManager.Delete(Util.ToInteger(cbx.Value)))
                iDeleted++;
        }

        // Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

        this.BindData();
    }

    protected void grvServiceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvServiceDetail.PageIndex = e.NewPageIndex;
        this.BindData();
    }

    protected void grvServiceDetail_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortExpression"].ToString() == e.SortExpression)
        {
            ViewState["SortDirection"] = ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
        }
        else
        {
            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = SortDirection.Ascending;
        }
        BindData();
    }

    private void BindData()
    {
        try
        {
            DataTable dt = ServiceDetailsManager.SelectAllLottery();

            if (dt != null && dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;

                dv.Sort = Util.GetSortClause(ViewState["SortExpression"].ToString(), (SortDirection)ViewState["SortDirection"]);
                if (ViewState["SortExpression"].ToString() != "ServiceDetailID")
                {
                    dv.Sort += ", " + Util.GetSortClause("ServiceDetailID", SortDirection.Ascending);
                }

                this.grvServiceDetail.DataSource = dv;
                this.grvServiceDetail.DataBind();
                this.lblError.Text = "Hệ thống có tổng số <b>" + dt.Rows.Count + "</b> dịch vụ đang chạy";
            }
            else
            {
                lblError.Text = "Hiện tại chưa có dịch vụ nào được cập nhật";
            }
        }
        catch { }
    }
}